home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / GNUChess4_0_58.lha / gnuchess-4.0 / src / new / dspcom.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  29KB  |  1,278 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. #include "ataks.h"
  25. extern short Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  26. extern char *version, *patchlevel;
  27. char mvstr[4][6];
  28. char *InPtr;
  29.  
  30.  
  31. #include <ctype.h>
  32. #include <signal.h>
  33. #ifdef MSDOS
  34. #include <dos.h>
  35. #include <conio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #else
  40. #include <sys/param.h>
  41. #include <sys/types.h>
  42. #include <sys/file.h>
  43. #include <sys/ioctl.h>
  44. #endif
  45.  
  46. void
  47. algbr (short int f, short int t, short int flag)
  48.  
  49.  
  50. /*
  51.  * Generate move strings in different formats.
  52.  */
  53.  
  54. {
  55.   int m3p;
  56.  
  57.   if (f != t)
  58.     {
  59.       /* algebraic notation */
  60.       mvstr[0][0] = cxx[column (f)];
  61.       mvstr[0][1] = rxx[row (f)];
  62.       mvstr[0][2] = cxx[column (t)];
  63.       mvstr[0][3] = rxx[row (t)];
  64.       mvstr[0][4] = mvstr[3][0] = '\0';
  65.       if (((mvstr[1][0] = pxx[board[f]]) == 'P') || (flag & promote))
  66.     {
  67.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  68.         {
  69.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  70.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  71.           m3p = 2;
  72.         }
  73.       else
  74.         /* pawn ate */
  75.         {
  76.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  77.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  78.           mvstr[2][2] = mvstr[0][3];
  79.           m3p = 3;        /* to row */
  80.         }
  81.       if (flag & promote)
  82.         {
  83.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  84.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  85. #ifdef CHESSTOOL
  86.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  87.           mvstr[3][1] = mvstr[0][1];
  88.           mvstr[3][2] = mvstr[0][2];
  89.           mvstr[3][3] = mvstr[0][3];
  90.           mvstr[3][4] = '\0';
  91. #endif
  92.         }
  93.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  94.     }
  95.       else
  96.     /* not a pawn */
  97.     {
  98.       mvstr[2][0] = mvstr[1][0];
  99.       mvstr[2][1] = mvstr[0][1];
  100.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  101.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  102.       mvstr[2][4] = mvstr[1][3] = '\0';
  103.       strcpy (mvstr[3], mvstr[2]);
  104.       mvstr[3][1] = mvstr[0][0];
  105.       if (flag & cstlmask)
  106.         {
  107.           if (t > f)
  108.         {
  109.           strcpy (mvstr[1], CP[5]);
  110.           strcpy (mvstr[2], CP[7]);
  111.         }
  112.           else
  113.         {
  114.           strcpy (mvstr[1], CP[6]);
  115.           strcpy (mvstr[2], CP[8]);
  116.         }
  117.         }
  118.     }
  119.     }
  120.   else
  121.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  122. }
  123.  
  124.  
  125. int
  126. VerifyMove (char *s, short int iop, short unsigned int *mv)
  127.  
  128. /*
  129.  * Compare the string 's' to the list of legal moves available for the
  130.  * opponent. If a match is found, make the move on the board.
  131.  */
  132.  
  133. {
  134.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  135.   static struct leaf xnode;
  136.   struct leaf *node;
  137.  
  138.   *mv = 0;
  139.   if (iop == 2)
  140.     {
  141.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  142.       return (false);
  143.     }
  144.   cnt = 0;
  145.   MoveList (opponent, 2);
  146.   pnt = TrPnt[2];
  147.   while (pnt < TrPnt[3])
  148.     {
  149.       node = &Tree[pnt++];
  150.       algbr (node->f, node->t, (short) node->flags);
  151.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  152.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  153.     {
  154.       cnt++;
  155.       xnode = *node;
  156.     }
  157.     }
  158.   if (cnt == 1)
  159.     {
  160.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  161.       if (SqAtakd (PieceList[opponent][0], computer))
  162.     {
  163.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  164. #if defined CHESSTOOL
  165.       printz (CP[15]);
  166. #else
  167. #ifdef NONDSP
  168. /* Illegal move in check */
  169.       printz (CP[77]);
  170.       printz ("\n");
  171. #else
  172. /* Illegal move in check */
  173.       ShowMessage (CP[77]);
  174. #endif
  175. #endif /* CHESSTOOL */
  176.       return (false);
  177.     }
  178.       else
  179.     {
  180.       if (iop == 1)
  181.         return (true);
  182.       UpdateDisplay (xnode.f, xnode.t, 0, (short) xnode.flags);
  183.       if ((board[xnode.t] == pawn)
  184.           || (xnode.flags & capture)
  185.           || (xnode.flags & cstlmask))
  186.         {
  187.           Game50 = GameCnt;
  188.           ZeroRPT ();
  189.         }
  190.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  191.       GameList[GameCnt].nodes = 0;
  192.       ElapsedTime (1);
  193.       GameList[GameCnt].time = (short) et;
  194.       if (TCflag)
  195.         {
  196.           TimeControl.clock[opponent] -= et;
  197.           --TimeControl.moves[opponent];
  198.         }
  199.       *mv = (xnode.f << 8) | xnode.t;
  200.       algbr (xnode.f, xnode.t, false);
  201.       return (true);
  202.     }
  203.     }
  204. #if defined CHESSTOOL
  205.   printz (CP[78]);
  206. #else
  207. #ifdef NONDSP
  208. /* Illegal move */
  209.   printz (CP[75], s);
  210. #ifdef DEBUG8
  211.   if (1)
  212.     {
  213.       FILE *D;
  214.       int r, c, l;
  215.       extern unsigned short int PrVar[];
  216.       D = fopen ("/tmp/DEBUG", "a+");
  217.       pnt = TrPnt[2];
  218.       fprintf (D, "resp = %d\n", ResponseTime);
  219.       fprintf (D, "iop = %d\n", iop);
  220.       fprintf (D, "matches = %d\n", cnt);
  221.       algbr (hint >> 8, hint & 0xff, (short) 0);
  222.       fprintf (D, "hint %s\n", mvstr[0]);
  223.       fprintf (D, "inout move is %s\n", s);
  224.       for (r = 1; PrVar[r]; r++)
  225.     {
  226.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (short) 0);
  227.       fprintf (D, " %s", mvstr[0]);
  228.     }
  229.       fprintf (D, "\n");
  230.       fprintf (D, "legal move are \n");
  231.       while (pnt < TrPnt[3])
  232.     {
  233.       node = &Tree[pnt++];
  234.       algbr (node->f, node->t, (short) node->flags);
  235.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  236.     }
  237.       fprintf (D, "\n current board is\n");
  238.       for (r = 7; r >= 0; r--)
  239.     {
  240.       for (c = 0; c <= 7; c++)
  241.         {
  242.           l = locn (r, c);
  243.           if (color[l] == neutral)
  244.         fprintf (D, " -");
  245.           else if (color[l] == white)
  246.         fprintf (D, " %c", qxx[board[l]]);
  247.           else
  248.         fprintf (D, " %c", pxx[board[l]]);
  249.         }
  250.       fprintf (D, "\n");
  251.     }
  252.       fprintf (D, "\n");
  253.       fclose (D);
  254.       abort ();
  255.     }
  256. #endif
  257. #else
  258. /* Illegal move */
  259.   ShowMessage (CP[76]);
  260. #endif
  261. #endif /* CHESSTOOL */
  262. #if !defined CHESSTOOL && !defined XBOARD
  263.   if (cnt > 1)
  264.     ShowMessage (CP[32]);
  265. #endif /* CHESSTOOL */
  266.   return (false);
  267. }
  268.  
  269. int
  270. parser (char *f, int side)
  271. {
  272.   int c1, r1, c2, r2;
  273.  
  274.   if (f[4] == 'o')
  275.     if (side == black)
  276.       return 0x3C3A;
  277.     else
  278.       return 0x0402;
  279.   else if (f[0] == 'o')
  280.     if (side == black)
  281.       return 0x3C3E;
  282.     else
  283.       return 0x0406;
  284.   else
  285.     {
  286.       c1 = f[0] - 'a';
  287.       r1 = f[1] - '1';
  288.       c2 = f[2] - 'a';
  289.       r2 = f[3] - '1';
  290.       return (locn (r1, c1) << 8) | locn (r2, c2);
  291.     }
  292.   /*NOTREACHED*/
  293. }
  294.  
  295. void
  296. GetGame (void)
  297. {
  298.   FILE *fd;
  299.   char fname[256], *p;
  300.   int c, i, j;
  301.   short sq;
  302. /* enter file name */
  303.   ShowMessage (CP[63]);
  304.   scanz ("%s", fname);
  305. /* chess.000 */
  306.   if (fname[0] == '\0')
  307.     strcpy (fname, CP[137]);
  308.   if ((fd = fopen (fname, "r")) != NULL)
  309.     {
  310.       NewGame ();
  311.       fgets (fname, 256, fd);
  312.       computer = opponent = white;
  313.       InPtr = fname;
  314.       skip ();
  315.       if (*InPtr == 'c')
  316.     computer = black;
  317.       else
  318.     opponent = black;
  319.       skip ();
  320.       skip ();
  321.       skip ();
  322.       Game50 = atoi (InPtr);
  323.       fgets (fname, 256, fd);
  324.       InPtr = &fname[14];
  325.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  326.       skip ();
  327.       skip ();
  328.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  329.       fgets (fname, 256, fd);
  330.       InPtr = &fname[11];
  331.       skipb ();
  332.       TCflag = atoi (InPtr);
  333.       skip ();
  334.       InPtr += 14;
  335.       skipb ();
  336.       OperatorTime = atoi (InPtr);
  337.       fgets (fname, 256, fd);
  338.       InPtr = &fname[11];
  339.       skipb ();
  340.       TimeControl.clock[white] = atoi (InPtr);
  341.       skip ();
  342.       skip ();
  343.       TimeControl.moves[white] = atoi (InPtr);
  344.       fgets (fname, 256, fd);
  345.       InPtr = &fname[11];
  346.       skipb ();
  347.       TimeControl.clock[black] = atoi (InPtr);
  348.       skip ();
  349.       skip ();
  350.       TimeControl.moves[black] = atoi (InPtr);
  351.       fgets (fname, 256, fd);
  352.       for (i = 7; i > -1; i--)
  353.     {
  354.       fgets (fname, 256, fd);
  355.       p = &fname[2];
  356.       InPtr = &fname[11];
  357.       skipb ();
  358.       for (j = 0; j < 8; j++)
  359.         {
  360.           sq = i * 8 + j;
  361.           if (*p == '.')
  362.         {
  363.           board[sq] = no_piece;
  364.           color[sq] = neutral;
  365.         }
  366.           else
  367.         {
  368.           for (c = 0; c < 8; c++)
  369.             {
  370.               if (*p == pxx[c])
  371.             {
  372.               board[sq] = c;
  373.               color[sq] = black;
  374.             }
  375.             }
  376.           for (c = 0; c < 8; c++)
  377.             {
  378.               if (*p == qxx[c])
  379.             {
  380.               board[sq] = c;
  381.               color[sq] = white;
  382.             }
  383.             }
  384.         }
  385.           p++;
  386.           Mvboard[sq] = atoi (InPtr);
  387.           skip ();
  388.         }
  389.     }
  390.       GameCnt = 0;
  391.       flag.regularstart = true;
  392.       Book = BOOKFAIL;
  393.       fgets (fname, 256, fd);
  394.       fgets (fname, 256, fd);
  395.       fgets (fname, 256, fd);
  396.       while (fgets (fname, 256, fd))
  397.     {
  398.       struct GameRec *g;
  399.       int side = computer;
  400.  
  401.       side = side ^ 1;
  402.       ++GameCnt;
  403.       InPtr = fname;
  404.       skipb ();
  405.       g = &GameList[GameCnt];
  406.       g->gmove = parser (InPtr, side);
  407.       skip ();
  408.       g->score = atoi (InPtr);
  409.       skip ();
  410.       g->depth = atoi (InPtr);
  411.       skip ();
  412.       g->nodes = atoi (InPtr);
  413.       skip ();
  414.       g->time = atoi (InPtr);
  415.       skip ();
  416.       g->flags = c = atoi (InPtr);
  417.       skip ();
  418.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  419.       skip ();
  420.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  421.       g->piece = no_piece;
  422.       g->color = neutral;
  423.       if (c & (capture | cstlmask))
  424.         {
  425.           if (c & capture)
  426.         {
  427.           skip ();
  428.           for (c = 0; c < 8; c++)
  429.             if (pxx[c] == *InPtr)
  430.               break;
  431.           g->piece = c;
  432.         }
  433.           skip ();
  434.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  435.         }
  436.     }
  437.       /* GameCnt--; */
  438.       if (TimeControl.clock[white] > 0)
  439.     TCflag = true;
  440.       fclose (fd);
  441.     }
  442.   ZeroRPT ();
  443.   InitializeStats ();
  444.   UpdateDisplay (0, 0, 1, 0);
  445.   Sdepth = 0;
  446.   hint = 0;
  447. }
  448.  
  449. void
  450. GetXGame (void)
  451. {
  452.   FILE *fd;
  453.   char fname[256], *p;
  454.   int c, i, j;
  455.   short sq;
  456. /* Enter file name */
  457.   ShowMessage (CP[63]);
  458.   scanz ("%s", fname);
  459.   if (fname[0] == '\0')
  460. /* xboard.position.read*/
  461.     strcpy (fname, CP[205]);
  462.   if ((fd = fopen (fname, "r")) != NULL)
  463.     {
  464.       NewGame ();
  465.       flag.regularstart = false;
  466.       Book = false;
  467.       fgets (fname, 256, fd);
  468.       fname[6] = '\0';
  469.       if (strcmp (fname, CP[206]))
  470.     return;
  471.       fgets (fname, 256, fd);
  472.       fgets (fname, 256, fd);
  473.       for (i = 7; i > -1; i--)
  474.     {
  475.       fgets (fname, 256, fd);
  476.       p = fname;
  477.       for (j = 0; j < 8; j++)
  478.         {
  479.           sq = i * 8 + j;
  480.           if (*p == '.')
  481.         {
  482.           board[sq] = no_piece;
  483.           color[sq] = neutral;
  484.         }
  485.           else
  486.         {
  487.           for (c = 0; c < 8; c++)
  488.             {
  489.               if (*p == qxx[c])
  490.             {
  491.               board[sq] = c;
  492.               color[sq] = black;
  493.             }
  494.             }
  495.           for (c = 0; c < 8; c++)
  496.             {
  497.               if (*p == pxx[c])
  498.             {
  499.               board[sq] = c;
  500.               color[sq] = white;
  501.             }
  502.             }
  503.         }
  504.           p += 2;
  505.         }
  506.     }
  507.       fclose (fd);
  508.     }
  509.   ZeroRPT ();
  510.   InitializeStats ();
  511.   UpdateDisplay (0, 0, 1, 0);
  512.   Sdepth = 0;
  513.   hint = 0;
  514. }
  515.  
  516. void
  517. SaveGame (void)
  518. {
  519.   FILE *fd;
  520.   char fname[256];
  521.   short sq, i, c, f, t;
  522.   char p;
  523.  
  524.   if (savefile[0])
  525.     strcpy (fname, savefile);
  526.   else
  527.     {
  528. /* Enter file name*/
  529.       ShowMessage (CP[63]);
  530.       scanz ("%s", fname);
  531.     }
  532.  
  533.   if (fname[0] == '\0')
  534. /* chess.000 */
  535.     strcpy (fname, CP[137]);
  536.   if ((fd = fopen (fname, "w")) != NULL)
  537.     {
  538.       char *b, *w;
  539.  
  540.       b = w = CP[74];
  541.       if (computer == black)
  542.     b = CP[141];
  543.       if (computer == white)
  544.     w = CP[141];
  545.       fprintf (fd, CP[37], b, w, Game50);
  546.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  547.       fprintf (fd, CP[111], TCflag, OperatorTime);
  548.       fprintf (fd, CP[117],
  549.            TimeControl.clock[white], TimeControl.moves[white],
  550.            TimeControl.clock[black], TimeControl.moves[black]);
  551.       for (i = 7; i > -1; i--)
  552.     {
  553.       fprintf (fd, "%1d ", i + 1);
  554.       for (c = 0; c < 8; c++)
  555.         {
  556.           sq = i * 8 + c;
  557.           switch (color[sq])
  558.         {
  559.         case black:
  560.           p = pxx[board[sq]];
  561.           break;
  562.         case white:
  563.           p = qxx[board[sq]];
  564.           break;
  565.         default:
  566.           p = '.';
  567.         }
  568.           fprintf (fd, "%c", p);
  569.         }
  570.       for (f = i * 8; f < i * 8 + 8; f++)
  571.         fprintf (fd, " %d", Mvboard[f]);
  572.       fprintf (fd, "\n");
  573.     }
  574.       fprintf (fd, "  %s\n", cxx);
  575.       fprintf (fd, CP[126]);
  576.       for (i = 1; i <= GameCnt; i++)
  577.     {
  578.       struct GameRec *g = &GameList[i];
  579.  
  580.       f = g->gmove >> 8;
  581.       t = (g->gmove & 0xFF);
  582.       algbr (f, t, g->flags);
  583.       fprintf (fd, "%s %5d %5d %7ld %5d %5d  %#08lx %#08lx  %c   %s\n",
  584.            mvstr[0], g->score, g->depth,
  585.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,
  586.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  587.     }
  588.       fclose (fd);
  589. /* Game saved */
  590.       ShowMessage (CP[70]);
  591.     }
  592.   else
  593.     /*ShowMessage ("Could not open file");*/
  594.     ShowMessage (CP[48]);
  595. }
  596.  
  597. void
  598. ListGame (void)
  599. {
  600.   FILE *fd;
  601.   short i, f, t;
  602.   long when;
  603.   char fname[256], dbuf[256];
  604.  
  605.   if (listfile[0])
  606.     strcpy (fname, listfile);
  607.   else
  608.     {
  609. #ifdef MSDOS
  610.       sprintf (fname, "chess.lst");
  611. #else
  612.       time (&when);
  613.       strncpy (dbuf, ctime (&when), 20);
  614.       dbuf[7] = '\0';
  615.       dbuf[10] = '\0';
  616.       dbuf[13] = '\0';
  617.       dbuf[16] = '\0';
  618.       dbuf[19] = '\0';
  619. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  620.    date is Jan 1
  621.    time is 02:03:04
  622.    program played black */
  623.       sprintf (fname, "CLp%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  624.       /* replace space padding with 0 */
  625.       for (i = 0; fname[i] != '\0'; i++)
  626.     if (fname[i] == ' ')
  627.       fname[i] = '0';
  628. #endif /* MSDOS */
  629.     }
  630.   fd = fopen (fname, "w");
  631.   if (!fd)
  632.     {
  633.       printf (CP[219], fname);
  634.       exit (1);
  635.     }
  636.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  637.   fprintf (fd, CP[161], patchlevel);
  638.   fprintf (fd, CP[10]);
  639.   fprintf (fd, CP[11]);
  640.   for (i = 1; i <= GameCnt; i++)
  641.     {
  642.       f = GameList[i].gmove >> 8;
  643.       t = (GameList[i].gmove & 0xFF);
  644.       algbr (f, t, GameList[i].flags);
  645.       fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  646.            GameList[i].score, GameList[i].depth,
  647.            GameList[i].nodes, GameList[i].time);
  648.       if ((i % 2) == 0)
  649.     {
  650.       fprintf (fd, "\n");
  651. #ifdef DEBUG40
  652.       if (computer == black)
  653.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  654.              GameList[i].d1,
  655.              GameList[i].d2,
  656.              GameList[i].d3,
  657.              GameList[i].d4,
  658.              GameList[i].d5,
  659.              GameList[i].d6,
  660.              GameList[i].d7);
  661.       else
  662.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  663.              GameList[i - 1].d1,
  664.              GameList[i - 1].d2,
  665.              GameList[i - 1].d3,
  666.              GameList[i - 1].d4,
  667.              GameList[i - 1].d5,
  668.              GameList[i - 1].d6,
  669.              GameList[i - 1].d7);
  670. #endif
  671.     }
  672.       else
  673.     fprintf (fd, "         ");
  674.     }
  675.   fprintf (fd, "\n\n");
  676.   if (GameList[GameCnt].flags & draw)
  677.     {
  678.       fprintf (fd, CP[54], DRAW);
  679.     }
  680.   else if (GameList[GameCnt].score == -9999)
  681.     {
  682.       fprintf (fd, "%s\n", ColorStr[player ]);
  683.     }
  684.   else if (GameList[GameCnt].score == 9998)
  685.     {
  686.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  687.     }
  688.   fclose (fd);
  689. }
  690.  
  691. void
  692. Undo (void)
  693.  
  694. /*
  695.  * Undo the most recent half-move.
  696.  */
  697.  
  698. {
  699.   short f, t;
  700.   f = GameList[GameCnt].gmove >> 8;
  701.   t = GameList[GameCnt].gmove & 0xFF;
  702.   if (board[t] == king && distance (t, f) > 1)
  703.     (void) castle (GameList[GameCnt].color, f, t, 2);
  704.   else
  705.     {
  706.       /* Check for promotion: */
  707.       if (GameList[GameCnt].flags & promote)
  708.     {
  709.       board[t] = pawn;
  710.     }
  711.       board[f] = board[t];
  712.       color[f] = color[t];
  713.       board[t] = GameList[GameCnt].piece;
  714.       color[t] = GameList[GameCnt].color;
  715.       if (color[t] != neutral)
  716.     Mvboard[t]--;
  717.       Mvboard[f]--;
  718.     }
  719.   if (GameList[GameCnt].flags & epmask)
  720.     EnPassant (otherside[color[f]], f, t, 2);
  721.   else
  722.     InitializeStats ();
  723.   if (TCflag && (TCmoves>1))
  724.     ++TimeControl.moves[color[f]];
  725.   hashkey = GameList[GameCnt].hashkey;
  726.   hashbd = GameList[GameCnt].hashbd;
  727.   GameCnt--;
  728.   computer = computer ^ 1;
  729.   opponent = opponent ^ 1;
  730.   flag.mate = false;
  731.   Sdepth = 0;
  732.   player = player ^ 1;
  733.   ShowSidetoMove ();
  734.   UpdateDisplay (0, 0, 1, 0);
  735.   if (flag.regularstart)
  736.     Book = BOOKFAIL;
  737. }
  738.  
  739. void
  740.  TestSpeed (void (*f) (short int side, short int ply), unsigned j)
  741. {
  742.   short i;
  743.   long cnt, rate, t1, t2;
  744.  
  745.   t1 = time (0);
  746.   for (i = 0; i < j; i++)
  747.     {
  748.       f (opponent, 2);
  749.     }
  750.   t2 = time (0);
  751.   cnt = j * (TrPnt[3] - TrPnt[2]);
  752.   if (t2 - t1)
  753.     et = (t2 - t1) * 100;
  754.   else
  755.     et = 1;
  756.   rate = (et) ? (cnt / et) : 0;
  757.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  758. #ifdef NONDSP
  759.   printz (CP[91], cnt, rate*100);
  760. #ifdef DEBUG9
  761.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  762.     {
  763.       struct leaf *node = &Tree[j];
  764.       algbr (node->f, node->t, node->flags);
  765.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  766.     }
  767. #endif
  768. #else
  769.   ShowNodeCnt (cnt);
  770. #endif
  771. }
  772.  
  773. void
  774.  TestPSpeed (short int (*f) (short int side), unsigned j)
  775. {
  776.   short i;
  777.   long cnt, rate, t1, t2;
  778.   short int t;
  779.  
  780.   t1 = time (0);
  781.   for (i = 0; i < j; i++)
  782.     {
  783.       (void) f (opponent);
  784.     }
  785.   t2 = time (0);
  786.   cnt = j;
  787.   if (t2 - t1)
  788.     et = (t2 - t1) * 100;
  789.   else
  790.     et = 1;
  791.   rate = (et) ? (cnt / et) : 0;
  792.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  793. #ifdef NONDSP
  794.   printz (CP[91], cnt, rate*100);
  795. #else
  796.   ShowNodeCnt (cnt);
  797. #endif
  798. }
  799.  
  800.  
  801. void
  802. SetMachineTime (char *s)
  803. {
  804.   char *time;
  805.   int m, t;
  806.   time = &s[strlen (CP[197])];
  807.   t = strtol (time, &time, 10);
  808.   m = strtol (time, &time, 10);
  809.   if (t)
  810.     TimeControl.clock[computer] = t;
  811.   if (m)
  812.     TimeControl.moves[computer] = m;
  813. #ifdef XBOARD
  814.   printz (CP[222], m, t);
  815. #endif
  816. }
  817.  
  818.  
  819. void
  820. InputCommand (void)
  821.  
  822. /*
  823.  * Process the users command. If easy mode is OFF (the computer is thinking
  824.  * on opponents time) and the program is out of book, then make the 'hint'
  825.  * move on the board and call SelectMove() to find a response. The user
  826.  * terminates the search by entering ^C (quit siqnal) before entering a
  827.  * command. If the opponent does not make the hint move, then set Sdepth to
  828.  * zero.
  829.  */
  830.  
  831. {
  832.   int i = 0;
  833.   short have_shown_prompt = false;
  834.   short ok, tmp;
  835.   unsigned short mv;
  836.   char s[80], sx[80];
  837.  
  838. #if defined CHESSTOOL
  839.   short normal = false;
  840.  
  841. #endif
  842.  
  843.   ok = flag.quit = false;
  844.   player = opponent;
  845.   ft = 0;
  846.   if (hint > 0 && !flag.easy && !flag.force)
  847.     if ((board[hint >> 8] != pawn) || ((row (hint & 0xFF) != 0) && (row (hint & 0xFF) != 7)))
  848.       {
  849.     fflush (stdout);
  850.     time0 = time ((long *) 0);
  851.     algbr ((short) hint >> 8, (short) hint & 0xFF, false);
  852.     strcpy (s, mvstr[0]);
  853.     tmp = epsquare;
  854. #ifdef DEBUG12
  855.     if (1)
  856.       {
  857.         FILE *D;
  858.         int r, c, l;
  859.         extern unsigned short int PrVar[];
  860.         extern struct leaf rootnode;
  861.         D = fopen ("/tmp/DEBUGA", "a+");
  862.         fprintf (D, "score = %d\n", rootnode.score);
  863.         fprintf (D, "inout move is %s\n", s);
  864.         for (r = 1; PrVar[r]; r++)
  865.           {
  866.         algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (short) 0);
  867.         fprintf (D, " %s", mvstr[0]);
  868.           }
  869.         fprintf (D, "\n current board is\n");
  870.         for (r = 7; r >= 0; r--)
  871.           {
  872.         for (c = 0; c <= 7; c++)
  873.           {
  874.             l = locn (r, c);
  875.             if (color[l] == neutral)
  876.               fprintf (D, " -");
  877.             else if (color[l] == white)
  878.               fprintf (D, " %c", qxx[board[l]]);
  879.             else
  880.               fprintf (D, " %c", pxx[board[l]]);
  881.           }
  882.         fprintf (D, "\n");
  883.           }
  884.         fprintf (D, "\n");
  885.         fclose (D);
  886.       }
  887. #endif
  888. #if !defined CHESSTOOL
  889.     if (flag.post)
  890.       GiveHint ();
  891. #endif
  892.     if (VerifyMove (s, 1, &mv))
  893.       {
  894.         Sdepth = 0;
  895. #ifdef QUIETBACKGROUND
  896. #ifdef NONDSP
  897.         PromptForMove ();
  898. #else
  899.         ShowSidetoMove ();
  900.         ShowPrompt ();
  901. #endif
  902.         have_shown_prompt = true;
  903. #endif /* QUIETBACKGROUND */
  904.         SelectMove (computer, 2);
  905.         VerifyMove (s, 2, &mv);
  906.         Sdepth = 0;
  907.       }
  908.     ft = (time ((long *) 0) - time0) * 100;
  909.     epsquare = tmp;
  910.       }
  911.   while (!(ok || flag.quit))
  912.     {
  913. #if defined CHESSTOOL
  914.       normal = false;
  915. #endif
  916.       player = opponent;
  917. #ifdef QUIETBACKGROUND
  918.       if (!have_shown_prompt)
  919.     {
  920. #endif /* QUIETBACKGROUND */
  921. #ifdef NONDSP
  922.       PromptForMove ();
  923. #else
  924.       ShowSidetoMove ();
  925.       ShowPrompt ();
  926. #endif
  927. #ifdef QUIETBACKGROUND
  928.     }
  929.       have_shown_prompt = false;
  930. #endif /* QUIETBACKGROUND */
  931. #ifdef NONDSP
  932.       s[0] = sx[0] = '\0';
  933.       while (!sx[0])
  934.     i = (int) gets (sx);
  935. #else
  936.       fflush (stdout);
  937.       i = (int) getstr (sx);
  938. #endif
  939.       sscanf (sx, "%s", s);
  940.       if (i == EOF)
  941.     ExitChess ();
  942.       if (s[0] == '\0')
  943.     continue;
  944.       if (strcmp (s, CP[131]) == 0)    /*bd*/
  945.     {
  946. #if defined CHESSTOOL || defined XBOARD
  947.       chesstool = 0;
  948. #endif /* CHESSTOOL */
  949.       ClrScreen ();
  950.       UpdateDisplay (0, 0, 1, 0);
  951. #if defined CHESSTOOL || defined XBOARD
  952.       chesstool = 1;
  953. #endif /* CHESSTOOL */
  954.     }
  955.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  956.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  957.     flag.quit = true;
  958.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  959.     {
  960.       flag.post = !flag.post;
  961.     }
  962.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  963.     EditBoard ();
  964. #ifdef NONDSP
  965.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  966.     SetupBoard ();
  967. #endif
  968.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  969.     {
  970. #if defined CHESSTOOL
  971.       computer = white;
  972.       opponent = black;
  973.       flag.force = false;
  974.       Sdepth = 0;
  975. #endif /* CHESSTOOL */
  976.       ok = true;
  977.     }
  978.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  979.     {
  980.       ok = true;
  981.       flag.force = false;
  982.       if (computer == white)
  983.         {
  984.           computer = black;
  985.           opponent = white;
  986.         }
  987.       else
  988.         {
  989.           computer = white;
  990.           opponent = black;
  991.         }
  992.     }
  993.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  994.     help ();
  995.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  996.     flag.material = !flag.material;
  997.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  998.     {flag.force = !flag.force; flag.bothsides = false;}
  999.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1000.     Book = Book ? 0 : BOOKFAIL;
  1001.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1002.     {
  1003.       NewGame ();
  1004.       UpdateDisplay (0, 0, 1, 0);
  1005.     }
  1006.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1007.     ListGame ();
  1008.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1009.     SelectLevel ();
  1010.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1011.     flag.hash = !flag.hash;
  1012.       else if (strcmp (s, CP[132]) == 0)    /*beep*/
  1013.     flag.beep = !flag.beep;
  1014.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1015.     {
  1016.       SetMachineTime (sx);
  1017.     }
  1018.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1019.     ChangeAlphaWindow ();
  1020.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1021.     ChangeBetaWindow ();
  1022.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1023.     flag.rcptr = !flag.rcptr;
  1024.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1025.     GiveHint ();
  1026.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1027.     {
  1028.       flag.bothsides = !flag.bothsides;
  1029.           flag.force = false;
  1030.       Sdepth = 0;
  1031.       ElapsedTime (1);
  1032.       SelectMove (opponent, 1);
  1033.       ok = true;
  1034.     }
  1035.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1036.     {
  1037.       flag.reverse = !flag.reverse;
  1038.       ClrScreen ();
  1039.       UpdateDisplay (0, 0, 1, 0);
  1040.     }
  1041.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1042.     {
  1043.       computer = computer ^ 1;
  1044.       opponent = opponent ^ 1;
  1045.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1046.       flag.force = false;
  1047.       Sdepth = 0;
  1048.       ok = true;
  1049.     }
  1050.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1051.     {
  1052.       computer = black;
  1053.       opponent = white;
  1054.       xwndw = WXWNDW;
  1055.       flag.force = false;
  1056.       Sdepth = 0;
  1057.  
  1058.       /*
  1059.            * ok = true; don't automatically start with white command
  1060.            */
  1061.     }
  1062.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1063.     {
  1064.       computer = white;
  1065.       opponent = black;
  1066.       xwndw = BXWNDW;
  1067.       flag.force = false;
  1068.       Sdepth = 0;
  1069.  
  1070.       /*
  1071.            * ok = true; don't automatically start with black command
  1072.            */
  1073.     }
  1074.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1075.     {
  1076.       Undo ();
  1077.     }
  1078.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1079.     {
  1080.       Undo ();
  1081.       Undo ();
  1082.     }
  1083.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1084.     GetGame ();
  1085.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1086.     GetXGame ();
  1087.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1088.     SaveGame ();
  1089.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1090.     ChangeSearchDepth ();
  1091. #ifdef DEBUG
  1092.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1093.     ChangeDbLev ();
  1094. #endif /* DEBUG */
  1095.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1096.     ChangeHashDepth ();
  1097.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1098.     dither = DITHER;
  1099.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1100.     flag.easy = !flag.easy;
  1101.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1102.     SetContempt ();
  1103.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1104.     ChangeXwindow ();
  1105.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1106.     {
  1107.       flag.rv = !flag.rv;
  1108.       UpdateDisplay (0, 0, 1, 0);
  1109.     }
  1110.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1111.     {
  1112.       flag.coords = !flag.coords;
  1113.       UpdateDisplay (0, 0, 1, 0);
  1114.     }
  1115.       else if (strcmp (s, CP[193]) == 0)    /*stras*/
  1116.     {
  1117.       flag.stars = !flag.stars;
  1118.       UpdateDisplay (0, 0, 1, 0);
  1119.     }
  1120.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1121.     {
  1122.       ShowMessage (CP[108]);/*test movelist*/
  1123.       TestSpeed (MoveList, 20000);
  1124.       ShowMessage (CP[107]);/*test capturelist*/
  1125.       TestSpeed (CaptureList, 30000);
  1126.       ShowMessage (CP[85]);/*test score position*/
  1127.       TestPSpeed (ScorePosition, 15000);
  1128.     }
  1129.       else
  1130.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1131.     ShowPostnValues ();
  1132.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1133.     DoDebug ();
  1134.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1135.         DoTable (Mwpawn);
  1136.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1137.         DoTable (Mbpawn);
  1138.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1139.         DoTable (Mknight[white]);
  1140.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1141.         DoTable (Mknight[black]);
  1142.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1143.         DoTable (Mbishop[white]);
  1144.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1145.         DoTable (Mbishop[black]);
  1146.       else
  1147.     {
  1148. #if defined CHESSTOOL
  1149.       normal = (ok = VerifyMove (s, 0, &mv));
  1150. #else
  1151.       ok = VerifyMove (s, 0, &mv);
  1152. #endif
  1153.       if ((ok && mv != hint))
  1154.         {
  1155.           Sdepth = 0;
  1156.           ft = 0;
  1157.         }
  1158.       else
  1159.         Sdepth = 0;
  1160.     }
  1161.     }
  1162.  
  1163.   ElapsedTime (1);
  1164.   if (flag.force)
  1165.     {
  1166.       computer = opponent;
  1167.       opponent = computer ^ 1;
  1168.     }
  1169. #if defined CHESSTOOL || defined XBOARD
  1170. #if defined CHESSTOOL
  1171.   if (normal)
  1172.     if (computer == white)
  1173.       printz ("%d. %s", ++mycnt2, s);
  1174.     else
  1175.       printz ("%d. ... %s", ++mycnt2, s);
  1176. #else
  1177.   printz ("%d. %s\n", ++mycnt2, s);
  1178. #endif
  1179. #ifdef notdef
  1180.   if (flag.post)
  1181.     {
  1182.       register int i;
  1183.  
  1184.       printz (" %6d ", MSCORE);
  1185.       for (i = 1; MV[i] > 0; i++)
  1186.     {
  1187.       algbr ((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
  1188.       printz ("%5s ", mvstr[0]);
  1189.     }
  1190.     }
  1191.   printz ("\n");
  1192. #endif
  1193. #endif /* CHESSTOOL */
  1194.   signal (SIGINT, TerminateSearch);
  1195. #ifndef MSDOS
  1196.   signal (SIGQUIT, TerminateSearch);
  1197. #endif /* MSDOS */
  1198. }
  1199.  
  1200. void
  1201. ElapsedTime (short int iop)
  1202.  
  1203.  
  1204. /*
  1205.  * Determine the time that has passed since the search was started. If the
  1206.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1207.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1208.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1209.  */
  1210.  
  1211. {
  1212. #ifndef MSDOS
  1213.   long nchar;
  1214.   extern int errno;
  1215.   int i;
  1216. #ifdef FIONREAD
  1217.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1218.     {
  1219.       perror ("FIONREAD");
  1220.       fprintf (stderr,
  1221.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1222.     i, errno, FIONREAD);
  1223.       exit (1);
  1224.     }
  1225.  
  1226.   if (nchar)
  1227.     {
  1228.       if (!flag.timeout)
  1229.     flag.musttimeout = true;
  1230.       flag.bothsides = false;
  1231.     }
  1232. #endif /*FIONREAD*/
  1233. #else
  1234.   if (kbhit ())
  1235.     {
  1236.       if (!flag.timeout)
  1237.     flag.musttimeout = true;
  1238.       flag.bothsides = false;
  1239.     }
  1240. #endif /* MSDOS */
  1241.   et = (time ((long *) 0) - time0) * 100;
  1242.   ETnodes = NodeCnt + ZNODES;
  1243.   if (et < 0)
  1244.     et = 0;
  1245.   if (iop == 1)
  1246.     {
  1247.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1248.     flag.timeout = true;
  1249.       ETnodes = NodeCnt + ZNODES;
  1250.       time0 = time ((long *) 0);
  1251.     }
  1252. #if !defined NONDSP
  1253. #ifdef QUIETBACKGROUND
  1254.   if (!background)
  1255. #endif /* QUIETBACKGROUND */
  1256.     UpdateClocks ();
  1257. #endif
  1258. }
  1259.  
  1260. void
  1261. SetTimeControl (void)
  1262. {
  1263.   if (TCflag)
  1264.     {
  1265.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1266.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1267.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1268.     }
  1269.   else
  1270.     {
  1271.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1272.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  1273.     }
  1274.   flag.onemove = (TCmoves == 1);
  1275.   et = 0;
  1276.   ElapsedTime (1);
  1277. }
  1278.